﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Edmx.Utility
{
    public class NotifyProperty<T>
    {
        private readonly PropertyChangedEventHandler _propertyChanged;
        private readonly PropertyChangingEventHandler _propertyChanging;
        private readonly string _name;

        private T _value;

        public NotifyProperty(string name, PropertyChangingEventHandler propertyChanging, PropertyChangedEventHandler propertyChanged)
        {
            _propertyChanged = propertyChanged;
            _propertyChanging = propertyChanging;
            _name = name;
        }

        public string Name
        {
            get { return _name; }
        }

        public T Value
        {
            get { return _value; }
            set
            {
                if (Equals(_value, value))
                    return;

                RaisePropertyChanging();
                _value = value;
                RaisePropertyChanged();
            }
        }

        private void RaisePropertyChanging()
        {
            if (_propertyChanging == null)
                return;

            var args = new PropertyChangingEventArgs(Name);
            _propertyChanging.Invoke(this, args);
        }

        private void RaisePropertyChanged()
        {
            if (_propertyChanged == null)
                return;

            var args = new PropertyChangedEventArgs(Name);
            _propertyChanged.Invoke(this, args);
        }
    }
}
